package it.koutifaris.travianmap;

import it.koutifaris.travianmap.mapsqlparser.MapSqlParser.RemoteSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.zip.GZIPInputStream;

public class TestRemoteSource implements RemoteSource {

	private final InputStream rawStream;
	private final boolean isGzip;
	private final Date lastModified;

	public TestRemoteSource(Date lastModified, InputStream rawStream, boolean isGzip) {
		this.rawStream = rawStream;
		this.isGzip = isGzip;
		this.lastModified = lastModified;
	}

	@Override
	public InputStream sourceStream(StreamWrapper streamWrapper) {
		if (isGzip) {
			return gzWrap(streamWrapper.wrap(rawStream));
		}

		return streamWrapper.wrap(rawStream);
	}

	private GZIPInputStream gzWrap(InputStream inputStream) {
		try {
			return new GZIPInputStream(inputStream);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Date lastModified() {
		return lastModified;
	}

}
